/***************************************************************************
 *   Copyright (C) 2009 by pawel   *
 *   pawel@byszewski.pl   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef RECDES_H
#define RECDES_H
#include <iostream>
#include <fstream>


#include "settings.h"


using namespace std;
/**
	@author pawel <pawel@byszewski.pl>
*/
class RecordDescriptor
{
	protected:
		char booked;
		unsigned int size;
		unsigned int ofset;
		//char type[ELEMENTCOUNT];
	public:
		char type[ELEMENTCOUNT];
		RecordDescriptor(){};
		void setSize(unsigned int s)
		{
			size=s;
		}

		void setBooked(char c)
		{	booked=c;
		}

		void setOfset( unsigned int i)
		{	ofset=i;
		}

		char getBooked()
		{	return booked;
		}

		unsigned int getSize()
		{	return size;
		}

		int getOfset()
		{	return ofset;
		}
		void setType(unsigned int i, char _type){
			type[i]=_type;
		}
		char* getType(){
			return type;
		}
		~RecordDescriptor(){};
		void toStream(fstream &f){
			f << booked;
			f << size;
			f << ofset;
		}

		char* createBuf(){
			char *buf=new char[DESLENGTH];
			buf[0]=booked;//booked
			char *temp=new char[4];
			temp=(char *)&size;

			cout <<"create size:"<<size;;
			for(int i=0;i<4;++i){
				//cout <<"dupa1 1 "<<temp[i]<<endl;//size
				buf[i+1]=temp[i];
			}
			cout <<" ofset: "<<ofset;
			temp=(char *)&ofset;
			for(int i=0;i<4;++i)//ofset
				buf[5+i]=temp[i];
			cout <<" typ: ";
			for(int i=0;i<ELEMENTCOUNT;++i){
				buf[9+i]=type[i];
				cout<<type[i];
			}
			cout <<endl;
			return buf;
			
		}
		void wypisz(){
			cout <<endl <<"****START***"<<endl;
			int k=(int)booked;
			cout <<"zaj: "<<k<<"  size: "<<size<<"  ofset: "<<getOfset() <<"   typ: ";
			for(int i =0; i<ELEMENTCOUNT;++i){
				int k=(int)type[i];
				cout <<k;
			}
			cout <<endl<<"*********END******"<<endl;
		}
		void createFromBuf(char *buf){
			booked=buf[0];
			//cout<<"create from buf ";
			unsigned int *_size=(unsigned int*)(buf+1);
			size=*_size;
			//cout<<" odczytany size: "<<*_size;
			unsigned int *_ofset=(unsigned int*)(buf+5);//bo tu sie zaczna ofset
			ofset=*_ofset;
			//cout<<"  odczytany ofset: "<<*_ofset<<endl;
			for(int i=0;i<ELEMENTCOUNT;++i)
				type[i]=buf[9+i];
		 
		}
		int isSimilar(RecordDescriptor *des){
			cout <<"\t isSimilar: ";
			//cout <<"\t";wypisz();
			//cout <<"\t";des->wypisz();
			int sukces=1;
			char *temp=new char[ELEMENTCOUNT];
			for(int i=0;i<ELEMENTCOUNT;++i) temp[i]=type[i];
			for(int i=0; i<ELEMENTCOUNT && des->type[i]!=9;++i){
			//	cout <<"porownuje: "<<(int)des->type[1]<<" z  *";
				for(int k=0;k<ELEMENTCOUNT;++k){
					//cout<<(int)temp[k]<<"*";
				      if(temp[k]==des->type[i]){
			//		      cout <<"znaleziono typ"<<endl;
				           temp[k]=9;//zeby drugi raz tego nie przeszukac
					   sukces=1;
					   break;
				      } 
				      sukces=0;     
				}
			 if(sukces==0){ return 0;};
			}
			//cout<<"\t koniec isSimilar dla deskryptora"<<endl;
			return 1;
		}


};

#endif
